/**
 * Created with IntelliJ IDEA.
 * Description:
 */
public class BinarySearchTree {
    public static class Node {
        private int value;
        private Node left;
        private Node right;

        public Node(int value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "value=" + value +
                    ", left=" + left +
                    ", right=" + right +
                    '}';
        }
    }

    //根节点
    public Node root = null;

    /**
     * 查找节点
     *
     * @param key
     * @return
     */
    public Node search(int key) {
        Node cur = root;
        while (cur != null) {
            if (cur.value < key) {
                cur = cur.right;
            } else if (cur.value > key) {
                cur = cur.left;
            } else {
                return cur;
            }
        }
        return null;
    }

    /**
     * 插入数据
     *
     * @param key
     * @return
     */
    public boolean insert(int key) {
        if (root == null) {
            root=new Node(key);
            return true;
        }
        Node pre = null;  //记录cur前一个位置
        Node cur = root;
        while (cur != null) {
            if (cur.value < key) {
                pre = cur;
                cur = cur.right;
            } else if (cur.value > key) {
                pre = cur;
                cur = cur.left;
            } else {                //二叉树不允许插入相同的值
                return false;
            }
        }
        Node node = new Node(key);
        if (pre.value > key) {
            pre.left = node;
        }
        if (pre.value < key) {
            pre.right = node;
        }
        return true;
    }

    public boolean remove(int key) {
        Node cur = root;
        Node pre = null;
        while (cur != null) {
            if (cur.value < key) {
                pre = cur;
                cur = cur.right;
            } else if (cur.value > key) {
                pre = cur;
                cur = cur.left;
            } else {
                removeNode(pre, cur);
                return true;
            }
        }
        return false;
    }

    private void removeNode(Node pre, Node cur) {
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (pre.left == cur) {
                pre.left = cur.right;
            } else {
                pre.right = cur.right;
            }
        } else if (cur.right == null) {
                if (cur == root) {
                    root = cur.left;
                } else if (pre.left == cur) {
                    pre.left = cur.left;
                } else {
                    pre.right = cur.left;
                }
            } else {
                    Node tmp=cur.left;
                    Node tpre=cur;
            while (tmp.right!=null){
                tpre=tmp;
                tmp=tmp.right;
            }
            cur.value=tmp.value;
            if (tpre.left==tmp){
                tpre.left=tmp.left;
            }else {
                tpre.right =tmp.left;
            }
        }
    }


}
